package com.fare.taluo.core.domain.analyse.monitor.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.analyse.monitor.FreightMonitorSettingsEntity;
import com.fare.taluo.core.domain.analyse.monitor.FreightMonitorSettingsEntityRepository;
import com.fare.taluo.util.StringUtils;
@Repository
public class FreightMonitorSettingsEntityRepositoryImpl extends EntityRepositoryHibernate implements FreightMonitorSettingsEntityRepository{
	//初始化页面
	@Override
	public List<FreightMonitorSettingsEntity> findFreightMonitorAll(String currentUser) throws Exception{
		List<Object> list = new ArrayList<Object>();
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String mydate=sdf.format(date);
		Date newdate=sdf.parse(mydate);
		StringBuffer sbf = new StringBuffer("from FreightMonitorSettingsEntity fm where 1=1");
		sbf.append(" and fm.currentUser =?");
		list.add(currentUser);
		sbf.append(" and fm.departDate >=?");
		list.add(newdate);
		sbf.append(" order by fm.opDate desc");
		List<FreightMonitorSettingsEntity> lists=null;
		try {
			lists =super.find(sbf.toString(),list.toArray(),FreightMonitorSettingsEntity.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		return lists;
	}
	//删除其中一条数据
	@Override
	public void delete(FreightMonitorSettingsEntity entity) {
		super.remove(entity);
	}
	//条件查询
	public List<FreightMonitorSettingsEntity> findFreightMonitorSettingsEntityByCondition(Date queryDate,Date toQueryDate,String fromCode,String toCode,Date createTime,Date toCreateTime,String currentUsers){
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer("from FreightMonitorSettingsEntity fm where 1=1");
		sbf.append(" and fm.currentUser = ?");
		Date todatDate=com.fare.taluo.util.DateUtils.getDates(new Date(),"yyyy-MM-dd");
		list.add(currentUsers);
		if(queryDate!=null && toQueryDate !=null){
			DateFormat date2String = new SimpleDateFormat("yyyy-MM-dd");
			String dates=date2String.format(queryDate);
			String dates2=date2String.format(toQueryDate);
			sbf.append("and fm.departDate between date_format(?,'%Y-%m-%d') and date_format(?,'%Y-%m-%d') ");
			list.add(dates);
			list.add(dates2);
		}
		
		if(StringUtils.isNotEmpty(fromCode)){
			sbf.append(" and fm.fromCode = ?");
			list.add(fromCode);
		}
		if(StringUtils.isNotEmpty(toCode)){
			sbf.append(" and fm.toCode = ?");
			list.add(toCode);
		}
		if(createTime !=null && toCreateTime !=null){
			DateFormat dateString = new SimpleDateFormat("yyyy-MM-dd");
			String createTimes=dateString.format(createTime);
			String toCreateTimes=dateString.format(toCreateTime);
			sbf.append(" and fm.opDate between date_format(?,'%Y-%m-%d') and date_format(?,'%Y-%m-%d') ");
			list.add(createTimes);
			list.add(toCreateTimes);
		}
	/*	if(toCreateTime !=null){
			DateFormat dateString = new SimpleDateFormat("yyyy-MM-dd");
			String toCreateTimes=dateString.format(toCreateTime);
			sbf.append("and fm.createTime < ?");
			list.add(toCreateTimes);
		}*/
		sbf.append(" and fm.departDate > ?");
		list.add(todatDate);
		sbf.append(" order by fm.opDate desc");
		List<FreightMonitorSettingsEntity> lists =super.find(sbf.toString(),list.toArray(),FreightMonitorSettingsEntity.class);
		
		return lists;
	}

}
